package module.common.util;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;

import library.common.util.CommonDialogUtils;
import module.common.R;

/**
 * Created by zhaoyuehai 2018/8/7
 */
public class DialogUtils {

    public static Dialog getProgressDialog(Context context) {
        //默认触摸不消失，返回键消失
        return DialogUtils.getProgressDialog(context, null, true, false);
    }

    public static Dialog getProgressDialog(Context context, String text) {
        //默认触摸不消失，返回键消失
        return DialogUtils.getProgressDialog(context, text, true, false);
    }

    public static Dialog getProgressDialog(Context context, @Nullable String text, boolean cancelable, boolean cancelTouch) {
        return CommonDialogUtils.getProgressDialog(context, text, cancelable, cancelTouch);
    }

    public static void dismissDialog(Dialog dialog) {
        if (dialog != null) dialog.dismiss();
    }

    /**
     * 显示网络异常弹框
     *
     * @param context                 Context
     * @param positiveOnClickListener 前往操作的监听
     * @param negativeOnClickListener 刷新操作的监听
     * @return AlertDialog
     */
    public static void showNetworkErrorDialog(final Context context, DialogInterface.OnClickListener positiveOnClickListener, DialogInterface.OnClickListener negativeOnClickListener) {
        AlertDialog dialog = new AlertDialog
                .Builder(context)
                .setMessage(R.string.text_network_exception)
                .setCancelable(true)
                .setPositiveButton(R.string.text_go, positiveOnClickListener)
                .setNegativeButton(R.string.text_retry, negativeOnClickListener)
                .show();
        dialog.setCanceledOnTouchOutside(false);
    }

    public static void showAlterDialog(Context context, @StringRes int messageId, @StringRes int negativeTextId, @StringRes int positiveTextId, DialogInterface.OnClickListener onPositiveClickListener, DialogInterface.OnClickListener onNegativeClickListener) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setMessage(messageId)
                .setNegativeButton(negativeTextId, onNegativeClickListener)
                .setPositiveButton(positiveTextId, onPositiveClickListener)
                .setCancelable(true)
                .create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }

    public static void showAlterDialog(Context context, String messageStr, @StringRes int positiveTextId, @StringRes int negativeTextId, DialogInterface.OnClickListener onPositiveClickListener, DialogInterface.OnClickListener onNegativeClickListener) {
        AlertDialog dialog = new AlertDialog.Builder(context)
                .setMessage(messageStr)
                .setNegativeButton(negativeTextId, onNegativeClickListener)
                .setPositiveButton(positiveTextId, onPositiveClickListener)
                .setCancelable(true)
                .create();
        dialog.setCanceledOnTouchOutside(false);
        dialog.show();
    }
}
